﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

using System.Web.SessionState;

namespace ZX.WebApp.Crm
{
    /// <summary>
    /// BSendSysNews 的摘要说明
    /// 2014/7/17   蔡龙丰
    /// 为datsysnews表服务
    /// 为我所以的js提供方法
    /// </summary>
    public class BSendSysNews : IHttpHandler, IRequiresSessionState
    {
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }


        public void ProcessRequest(HttpContext context)
        {
            string num = context.Request.QueryString["num"].ToString();
            switch (num)
            {
                case "d":
                    DeleteDatSysNews(context);
                    break;
                case "c":
                    DeleteDatSysNewsInfo(context);
                    break;
                case "t":
                    getDpmNameAll(context);
                    break;
                case "fanGang":
                    UpdateFanGang(context);
                    break;
                case "DaiGang":
                    UpdateDaiGang(context);
                    break;
                case "getUsers":    //返回当前用户的   userid 时间  名字
                    if (context.Session["User"] == null)
                    {
                        context.Response.ContentType = "text/plain";
                        context.Response.Write("{\"UserID\":\"F20F53A6-080B-4472-90A6-69E22D27317C\",\"time\":\"" + DateTime.Now + "\",\"empName\":\"系统管理员\"}");
                    }
                    else
                    {
                        ZX.EHR.Library.DatUser user = (context.Session["User"] as ZX.EHR.Library.DatUser);
                        context.Response.ContentType = "text/plain";
                        context.Response.Write("{\"UserID\":\"" + user.UserID + "\",\"time\":\"" + DateTime.Now + "\",\"empName\":\"" + user.UName + "\"}");
                    }
                    break;
                case "deleteV_Cst_customer"://删除客户信息【逻辑】
                    string sql1 = "update Cst_customer set IsExpire=0 where id='" + context.Request.QueryString["ID"].ToString() + "'";
                    context.Response.ContentType = "text/plain";
                    context.Response.Write(ExecuteNonQuery(sql1).ToString());
                    break;
                case "deleteV_Cst_activity"://删除客户来往记录【逻辑】
                    string sql2 = "update Cst_activity set IsDelete=1 where id='" + context.Request.QueryString["ID"].ToString() + "'";
                    context.Response.ContentType = "text/plain";
                    context.Response.Write(ExecuteNonQuery(sql2).ToString());
                    break;
               
                case "GetAuthority_btn":        ///获得当前用户的按钮权限
                    GetAuthority_btn(context, context.Request.QueryString["ID"].ToString());
                    break;
                case "GetCarld":        ///获得车辆的联动效果
                    GetCarld(context, context.Request.QueryString["ID"].ToString(), context.Request.QueryString["name"]);
                    break;
                case "GetSelect":       ///获得下拉框的值
                    GetSelect(context);
                    break;
                case "GetCust_no":      //获得今年最大的客户编号
                    GetCust_no(context);
                    break;
                case "GetHuiFangByID":    //根据客户和接触记录编号，获得回访结果
                    GetHuiFangByID(context);
                    break;
                case "UpdateCust_status":    //回访客户 成交状态为     已成交
                    UpdateCust_status(context);///要修改客户的   成交状态    已成交
                    break;
                case "GetCst_User":    //获得客户的总数量
                    GetCst_User(context);///获得客户的总数量
                    break;
                case "GetCst_Dept":    //获得员工的部门，及最近两月的客户情况
                    GetCst_Dept(context);
                    break;
                case "GetDBSelect":    //获得db下拉框的值【停放楼层、车身颜色】
                    GetDBSelect(context);
                    break;
                default:
                    break;
            }
        }


        /// <summary>
        /// 获得db下拉框的值【停放楼层、车身颜色】
        /// </summary>
        /// <param name="context"></param>
        private void GetDBSelect(HttpContext context)
        {
            //打开数据库
            string connString = System.Configuration.ConfigurationManager.ConnectionStrings["AKDDBConnString"].ToString();
            SqlConnection conn = new SqlConnection(connString);
            conn.Open();

            System.Text.StringBuilder selectStr = new System.Text.StringBuilder();

            //Dictionary<string, string> dicSelect = ZX.WebApp.Mobi.Customer.Authority.dicDbSelect;
            Dictionary<string, string> dicSelect = null;
            foreach (string key in dicSelect.Keys)
            {
                string sql = "select CodeName,CodeNO from Code_BaseInfo where CatNO='" + dicSelect[key] + "' order by OrderNO desc";
                DataSet ds = new DataSet();
                SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
                sda.Fill(ds);

                string json = ZX.Common.Globals.DataTableToJSON(ds.Tables[0]).Replace("\n", " ").Replace("\r", " ").Replace("null", "").Replace("[BR]", " ").Replace("@@@@", " ").Replace("@@", " ");
                if (!string.IsNullOrEmpty(json))
                {
                    selectStr.Append("\"" + key + "\":" + json + ",");
                }
                ds.Dispose();
                sda.Dispose();
                ds.Clear();
            }

            conn.Close();

            context.Response.ContentType = "text/plain";
            context.Response.Write("{" + selectStr.ToString().TrimEnd(',') + "}");
        }

        /// <summary>
        /// 获得员工的部门，及最近两月的客户情况
        /// </summary>
        /// <param name="context"></param>
        private void GetCst_Dept(HttpContext context)
        {
            System.Text.StringBuilder strCondition = new System.Text.StringBuilder();

            Guid? userID = null;
            string funId = context.Request.QueryString["funId"];
            if (context.Session["User"] == null)
            {
                userID = Guid.Parse("F20F53A6-080B-4472-90A6-69E22D27317C");
            }
            else
            {
                ZX.EHR.Library.DatUser user = (context.Session["User"] as ZX.EHR.Library.DatUser);
                userID = user.UserID;
            }

            ///根据用户编号和functionID，获取他的权限点
            IList<ZX.EHR.Library.SysRoleFunction> list = (new ZX.BLL.BUser()).GetRoleFunction(userID, Guid.Parse(funId));

            if (list == null)
            {
                strCondition.Append("{\"type\":0}");
            }
            else
            {
                foreach (var entity in list)
                {
                    #region 自定义条件
                    switch (entity.OperateRangeType.Trim())
                    {
                        ///只能查看本人数据
                        case "0":
                            if (strCondition.ToString() != "")
                            {
                                strCondition.Append(" OR ");
                            }
                            strCondition.Append(string.Format(" AddUser='{0}'", userID));
                            break;
                        ///可以看本部门的
                        case "1":
                            if (strCondition.ToString() != "")
                            {
                                strCondition.Append(" OR ");
                            }
                            strCondition.Append(string.Format(" AddUser in (select UserID from dbo.fn_GetUserDeptUsers('{0}'))", userID));
                            break;
                        // 全部数据
                        case "2":
                            if (strCondition.ToString() != "")
                            {
                                strCondition.Append(" OR ");
                            }
                            strCondition.Append(" 1=1");
                            break;
                        //自定义部门条件
                        case "3":
                            // 如果自定义条件
                            if (strCondition.ToString() != "")
                            {
                                strCondition.Append(" OR ");
                            }
                            strCondition.Append(string.Format(" AddUser in (select UserID from dbo.fn_GetDeptsUsers('{0}'))", entity.Depts));
                            break;
                        // 自定义条件数据
                        case "4":
                            if (strCondition.ToString() != "")
                            {
                                strCondition.Append(" OR ");
                            }
                            if (entity.Condition != string.Empty)
                            {
                                strCondition.Append(" " + entity.Condition + " ");
                            }
                            break;
                        default:
                            break;
                    }
                    #endregion
                }
            }
            string orders = context.Request.QueryString["orders"];
            ///获取经过过滤的  部门
            string sql = "select * from V_cst_dept where DeptName in(select distinct DeptName from V_cst_user " + (string.IsNullOrEmpty(strCondition.ToString()) ? "" : " where " + strCondition) + ")" + (orders == "" ? "" : " order by " + orders);


            DataTable table = GetDataTable(sql);
            ///要输出的json
            string json = ZX.Common.Globals.DataTableToJSON(table).Replace("\n", " ").Replace("\r", " ").Replace("null", "").Replace("[BR]", " ").Replace("@@@@", " ").Replace("@@", " ");
            string strFormat = "{\"total\":" + table.Rows.Count.ToString() + ",\"rows\":" + json + "}";

            context.Response.ContentType = "text/plain";
            context.Response.Write(strFormat.ToString());
        }

        /// <summary>
        /// 判断员工是否有报表的权限
        /// </summary>
        /// <param name="context"></param>
        private void GetCst_User(HttpContext context)
        {
            System.Text.StringBuilder strCondition = new System.Text.StringBuilder();

            Guid? userID = null;
            string funId = context.Request.QueryString["funId"];
            if (context.Session["User"] == null)
            {
                userID = Guid.Parse("F20F53A6-080B-4472-90A6-69E22D27317C");
            }
            else
            {
                ZX.EHR.Library.DatUser user = (context.Session["User"] as ZX.EHR.Library.DatUser);
                userID = user.UserID;
            }

            ///根据用户编号和functionID，获取他的权限点
            IList<ZX.EHR.Library.SysRoleFunction> list = (new ZX.BLL.BUser()).GetRoleFunction(userID, Guid.Parse(funId));

            if (list == null)
            {
                strCondition.Append("{\"type\":0}");
            }
            else
            {
                foreach (var entity in list)
                {
                    strCondition.Append("{\"OperateRangeType\":" + entity.OperateRangeType.Trim() + "}");
                }
            }
            context.Response.ContentType = "text/plain";
            context.Response.Write(strCondition.ToString());
        }

        /// <summary>
        /// 回访客户 成交状态为     已成交
        /// 要修改客户的   成交状态    已成交
        /// </summary>
        /// <param name="context"></param>
        private void UpdateCust_status(HttpContext context)
        {
            string cust_ID = context.Request.QueryString["ID"];
            string sql = "update Cst_customer set Cust_status=1 where ID='" + cust_ID + "'";
            int result = ExecuteNonQuery(sql);
            context.Response.ContentType = "text/plain";
            context.Response.Write(result.ToString());
        }

        /// <summary>
        /// 根据客户和接触记录编号，获得回访结果
        /// </summary>
        /// <param name="context"></param>
        private void GetHuiFangByID(HttpContext context)
        {
            //打开数据库
            string connString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ToString();
            SqlConnection conn = new SqlConnection(connString);
            conn.Open();

            ///要返回的html代码
            System.Text.StringBuilder selectStr = new System.Text.StringBuilder();
            //string sql = string.Format("select ISNULL(huifang_content,'') huifang_content,ISNULL(huifang_next,'') huifang_next, ISNULL(h.Cust_status,'') Cust_status,c.Cust_status cstCust_status from Cst_activity_huifang h left join Cst_customer c on h.CstID =c.ID where CstID='{0}' and AtvID='{1}' order by AddTime", context.Request.QueryString["CstID"], context.Request.QueryString["AtvID"]);
            string sql = string.Format("select * from V_Cst_activity_huifang where CstID='{0}' order by AddTime", context.Request.QueryString["CstID"]);
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            sda.Fill(ds);

            ///要输出的json
            string json = ZX.Common.Globals.DataTableToJSON(ds.Tables[0]).Replace("\n", " ").Replace("\r", " ").Replace("null", "").Replace("[BR]", " ").Replace("@@@@", " ").Replace("@@", " ");
            string strFormat = "{\"total\":" + ds.Tables[0].Rows.Count.ToString() + ",\"rows\":" + json + "}";

            sda.Dispose();
            ds.Clear();
            conn.Close();
            context.Response.ContentType = "text/plain";
            context.Response.Write(strFormat.ToString());
        }

        /// <summary>
        /// 获得下拉框的值
        /// </summary>
        /// <param name="context"></param>
        private void GetSelect(HttpContext context)
        {
            //打开数据库
            string connString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ToString();
            SqlConnection conn = new SqlConnection(connString);
            conn.Open();

            System.Text.StringBuilder selectStr = new System.Text.StringBuilder();

            //Dictionary<string, string> dicSelect = ZX.WebApp.Mobi.Customer.Authority.dicSelect;
            Dictionary<string, string> dicSelect = null;
            foreach (string key in dicSelect.Keys)
            {
                string sql = "select PhraseText,PhraseValue from DatPhraseDetail where ClassID='" + key + "' order by seq";
                DataSet ds = new DataSet();
                SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
                sda.Fill(ds);

                string json = ZX.Common.Globals.DataTableToJSON(ds.Tables[0]).Replace("\n", " ").Replace("\r", " ").Replace("null", "").Replace("[BR]", " ").Replace("@@@@", " ").Replace("@@", " ");
                if (!string.IsNullOrEmpty(json))
                {
                    selectStr.Append("\"" + dicSelect[key] + "\":" + json + ",");
                }
                sda.Dispose();
                ds.Clear();
            }

            conn.Close();

            context.Response.ContentType = "text/plain";
            context.Response.Write("{" + selectStr.ToString().TrimEnd(',') + "}");
        }

        /// <summary>
        /// 获得今年最大的客户编号
        /// </summary>
        /// <param name="context"></param>
        private void GetCust_no(HttpContext context)
        {
            string sql = "select 'AKD'+CONVERT(varchar(10),getdate(),112)+right(cast(power(10,6) as varchar)+(COUNT(*)+1),6) Cust_no from Cst_customer where DATEDIFF(yyyy,CreatedOn,GETDATE())=0";

            string connString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ToString();
            SqlConnection conn = new SqlConnection(connString);
            conn.Open();
            SqlCommand comm = new SqlCommand(sql, conn);
            string Cust_no = comm.ExecuteScalar().ToString();
            //释放资源
            comm.Dispose();
            conn.Close();
            context.Response.ContentType = "text/plain";
            context.Response.Write(Cust_no);
        }

        /// <summary>
        /// 获得车品牌联动
        /// </summary>
        /// <param name="context"></param>
        /// <param name="ID"></param>
        /// <param name="name"></param>
        private void GetCarld(HttpContext context, string ID, string name)
        {
            System.Text.StringBuilder sql = new System.Text.StringBuilder();
            switch (name)
            {
                case "MasterBrand":
                    sql.Append("select ID,Name=(ISNULL(fontspan, ''))+' '+[Name] from MasterBrand where IsEnabled=1");
                    if (ID != "")
                    {
                        sql.Append(" and Id='" + ID + "'");
                    }
                    sql.Append(" order by Interested desc, fontspan Asc, Name Asc");
                    break;
                case "Make":
                    sql.Append("select ID,Name from Make where IsEnabled=1");
                    if (ID != "")
                    {
                        sql.Append(" and MasterBrandId='" + ID + "'");
                    }
                    sql.Append(" order by Name");
                    break;
                case "Model":
                    sql.Append("select ID,Name from Model where IsEnabled=1");
                    if (ID != "")
                    {
                        sql.Append(" and MakeId='" + ID + "'");
                    }
                    sql.Append(" order by Name");
                    break;
                case "Style":
                    sql.Append("select Name=Convert(varchar(10),ssYear)+'款 '+ssName+' '+Emissions,ssId ID From Style where ssIsEnabled=1");
                    if (ID != "")
                    {
                        sql.Append(" and ssModelId='" + ID + "'");
                    }
                    sql.Append(" order by Name desc");
                    break;
                default:
                    break;
            }

            #region 打开数据库
            string connString = System.Configuration.ConfigurationManager.ConnectionStrings["AKDDBConnString"].ToString();
            SqlConnection conn = new SqlConnection(connString);
            conn.Open();
            SqlCommand com = new SqlCommand(sql.ToString(), conn);
            SqlDataReader dr = com.ExecuteReader();
            //保存要显示的html
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("<option value=\"\">请选择</option>");
            while (dr.Read())
            {
                sb.Append("<option value=\"" + dr["ID"] + "\">" + dr["Name"] + "</option>");
            }
            #endregion


            //释放资源
            dr.Dispose();
            com.Dispose();
            conn.Close();
            context.Response.ContentType = "text/plain";
            context.Response.Write(sb.ToString());
        }

        /// <summary>
        /// 根据用户编号和functionID来判断用户有没有
        /// 增删改查的权限
        /// </summary>
        /// <param name="context"></param>
        /// <param name="ID"></param>
        private void GetAuthority_btn(HttpContext context, string ID)
        {
            System.Text.StringBuilder sb1 = new System.Text.StringBuilder();
            string userID = "";
            if (context.Session["User"] == null)
            {
                userID = "F20F53A6-080B-4472-90A6-69E22D27317C";
            }
            else
            {
                ZX.EHR.Library.DatUser user = (context.Session["User"] as ZX.EHR.Library.DatUser);
                userID = user.UserID.ToString();
            }

            ///获得当前用户的按钮权限
            string[] Authority_btn = new ZX.BLL.BUser().GetFunctions(userID, ID);
            for (int i = 0; i < Authority_btn.Length; i++)
            {
                sb1.Append(Authority_btn[i] + ",");
            }

            context.Response.ContentType = "text/plain";
            context.Response.Write(sb1.ToString().TrimStart(',').TrimEnd(','));
        }

        
        /// <summary>
        /// 把在职员工改为待岗
        /// </summary>
        /// <param name="ID">选择员工的编号</param>
        /// <param name="context"></param>
        public void UpdateDaiGang(HttpContext context)
        {
            //获得id
            string ID = context.Request.QueryString["ID"].ToString();
            #region 访问数据库代码
            //打开数据库
            string connString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ToString();
            try
            {
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    conn.Open();
                    //系统日志
                    string logStr = "insert into SysDataLog ([Action],[Data],[DataLogID],[EntityName],[LogTime],[UserID]) ";

                    string UserID = (context.Session["User"] as ZX.EHR.Library.DatUser).UserID.ToString();
                    string Data = "\"EmployeeStatus\":\"6\",\"ID\":\"" + ID + "\"";

                    string EntityName = "hrEmployee";
                    DateTime dt = DateTime.Now;

                    //编辑员工状态    [hremployee.EmployeeStatus] 1.在职；2.不在职，3.离职，4.离退休,5，预审核、6待岗
                    string hrStr = "update hremployee set EmployeeStatus=6 where ID = '" + ID + "';";

                    ///系统日志 
                    string logStrs = hrStr + logStr + "values('update','{" + Data + "}',newid(),'" + EntityName + "',GETDATE(),'" + UserID + "');";

                    //设置生效日期【当前时间前一天】       [datuser.EffectDate]
                    //失效日期+30年                        [datuser.ExpireDate]
                    string datStr = "update Datuser set EffectDate='" + dt.ToString() + "',ExpireDate='" + dt.ToString() + "'";
                    datStr += " where EmpID='" + ID + "';";

                    Data = "\"EffectDate\":\"" + dt.ToString() + "\",\"ExpireDate\":\"" + dt.ToString() + "\",\"EmpID\":\"" + ID + "\"";

                    EntityName = "DatUser";
                    logStrs = logStrs + datStr + logStr + "values('update','{" + Data + "}',newid(),'" + EntityName + "',GETDATE(),'" + UserID + "')";

                    string s = logStrs.ToString();
                    SqlCommand com = new SqlCommand(s, conn);

                    int i = com.ExecuteNonQuery();

                    //释放资源
                    com.Dispose();
                    conn.Close();
                    context.Response.ContentType = "text/plain";
                    context.Response.Write(i.ToString());
                }
            }
            catch (Exception)
            {
                throw;
            }
            #endregion

        }

        /// <summary>
        /// 返岗
        /// 把待岗员工恢复为在职
        /// </summary>
        /// <param name="ID">选择员工的编号</param>
        /// <param name="context"></param>
        public void UpdateFanGang(HttpContext context)
        {
            //获得id
            string ID = context.Request.QueryString["ID"].ToString();
            #region 访问数据库代码
            //打开数据库
            string connString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ToString();
            try
            {
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    conn.Open();
                    //系统日志
                    string logStr = "insert into SysDataLog ([Action],[Data],[DataLogID],[EntityName],[LogTime],[UserID]) ";

                    string UserID = (context.Session["User"] as ZX.EHR.Library.DatUser).UserID.ToString();
                    string Data = "\"EmployeeStatus\":\"1\",\"ID\":\"" + ID + "\"";
                    //Data = @"""EmployeeStatus"":""1"",""ID"":""" + ID + "";
                    //Data = Data + "\"";

                    string EntityName = "hrEmployee";
                    DateTime dt = DateTime.Now;

                    //编辑员工状态    [hremployee.EmployeeStatus] 1.在职；2.不在职，3.离职，4.离退休,5，预审核、6待岗
                    string hrStr = "update hremployee set EmployeeStatus=1 where ID = '" + ID + "';";

                    ///系统日志 
                    string logStrs = hrStr + logStr + "values('update','{" + Data + "}',newid(),'" + EntityName + "',GETDATE(),'" + UserID + "');";

                    //设置生效日期【当前时间前一天】       [datuser.EffectDate]
                    //失效日期+30年                        [datuser.ExpireDate]
                    string datStr = "update Datuser set EffectDate='" + dt.ToString() + "',ExpireDate='" + dt.AddYears(30) + "'";
                    datStr += " where EmpID='" + ID + "';";

                    Data = "\"EffectDate\":\"" + dt.ToString() + "\",\"ExpireDate\":\"" + dt.AddYears(30) + "\",\"EmpID\":\"" + ID + "\"";
                    //Data = "\"EffectDate\":\"" + dt.ToString() + "\",\"ExpireDate\":\"" + dt.AddYears(30) + ")\",\"EmpID\":\"" + ID + "\"";

                    //Data = @"""EffectDate"":""" + dt.ToString() + "";
                    //Data = Data + "\"";
                    //Data = Data + @",""ExpireDate"":""" + dt.AddYears(30) + "";
                    //Data = Data + "\"";
                    //Data = Data + @",""EmpID"":""" + ID + "";
                    //Data = Data + "\"";

                    EntityName = "DatUser";
                    logStrs = logStrs + datStr + logStr + "values('update','{" + Data + "}',newid(),'" + EntityName + "',GETDATE(),'" + UserID + "')";

                    string s = logStrs.ToString();
                    SqlCommand com = new SqlCommand(s, conn);

                    int i = com.ExecuteNonQuery();

                    com.Dispose();
                    conn.Close();
                    context.Response.ContentType = "text/plain";
                    context.Response.Write(i.ToString());
                }
            }
            catch (Exception)
            {
                throw;
            }
            #endregion
        }

        /// <summary>
        /// 获取所有的部门及它的编码
        /// </summary>
        /// <param name="DmpName"></param>
        /// <param name="context"></param>
        private void getDpmNameAll(HttpContext context)
        {
            #region 访问数据库代码
            //打开数据库
            string connString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ToString();
            try
            {
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    conn.Open();
                    SqlCommand com = new SqlCommand("select DISTINCT DepartmentName,DepartmentCode from hrEmployeeContacts where checkNum =0 order by DepartmentCode", conn);

                    string s = "{\"rows\":[";
                    SqlDataReader dr = com.ExecuteReader();

                    while (dr.Read())
                    {
                        string name = dr["DepartmentName"].ToString();
                        string code = dr["DepartmentCode"].ToString();
                        s = s + "{\"name\":\"" + name + "\",\"code\":\"" + code + "\"},";
                    }

                    s = s.TrimEnd(',') + "]}";

                    dr.Dispose();
                    com.Dispose();
                    conn.Close();
                    context.Response.ContentType = "text/plain";
                    context.Response.Write(s.ToString());
                }
            }
            catch (Exception)
            {
                throw;
            }
            #endregion
        }

        /// <summary>
        /// 删除方法【逻辑删除】
        /// 步骤：根据信息编号，修改信息主表状态
        /// </summary>
        /// <returns></returns>
        public void DeleteDatSysNews(HttpContext context)
        {
            //获得id
            string ID = context.Request.QueryString["ID"].ToString();
            #region 访问数据库代码
            //打开数据库
            string connString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ToString();
            try
            {
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    conn.Open();
                    SqlCommand com = new SqlCommand("update DatSysNews set ISDelete =1 where ID=@ID", conn);
                    com.Parameters.Add("ID", SqlDbType.UniqueIdentifier);
                    com.Parameters["ID"].Value = Guid.Parse(ID);

                    int i = com.ExecuteNonQuery();

                    com.Dispose();
                    conn.Close();
                    context.Response.ContentType = "text/plain";
                    context.Response.Write(i.ToString());
                }
            }
            catch (Exception)
            {
                throw;
            }
            #endregion
        }


        /// <summary>
        /// 删除方法【逻辑删除】
        /// 步骤：根据信息编号，修改信息祥表表状态
        /// </summary>
        /// <returns></returns>
        public void DeleteDatSysNewsInfo(HttpContext context)
        {
            //获得id
            string ID = context.Request.QueryString["ID"].ToString();
            #region 访问数据库代码
            //打开数据库
            string connString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ToString();
            try
            {
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    conn.Open();
                    SqlCommand com = new SqlCommand("update DatSysNewsInfo set ISDelete =1 where ID=@ID", conn);
                    com.Parameters.Add("ID", SqlDbType.UniqueIdentifier);
                    com.Parameters["ID"].Value = Guid.Parse(ID);

                    int i = com.ExecuteNonQuery();

                    com.Dispose();
                    conn.Close();
                    context.Response.ContentType = "text/plain";
                    context.Response.Write(i.ToString());
                }
            }
            catch (Exception)
            {
                throw;
            }
            #endregion
        }

        #region 操纵数据库的公用方法
        /// <summary>
        /// 获取首行首列
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="connStrName">ConnectionStrings的名字</param>
        /// <returns></returns>
        private object GetExecuteScalar(string sql, string connStrName)
        {
            object o = null;
            //打开数据库
            string connString = System.Configuration.ConfigurationManager.ConnectionStrings[connStrName].ToString();
            SqlConnection conn = new SqlConnection(connString);
            conn.Open();

            SqlCommand comm = new SqlCommand(sql, conn);
            o = comm.ExecuteScalar();
            comm.Dispose();
            conn.Close();
            return o;
        }

        /// <summary>
        /// 根据sql，获得DataTable
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        private DataTable GetDataTable(string sql)
        {
            #region 访问数据库代码
            //打开数据库
            string connString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ToString();
            try
            {
                SqlConnection conn = new SqlConnection(connString);
                conn.Open();
                SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
                DataSet ds = new DataSet();
                sda.Fill(ds);
                DataTable table = ds.Tables[0];

                sda.Dispose();
                conn.Close();
                return table;

            }
            catch (Exception)
            {
                throw;
            }
            #endregion
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        private SqlDataReader ExecuteReader(string sql)
        {
            #region 访问数据库代码
            //打开数据库
            string connString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ToString();
            try
            {
                SqlConnection conn = new SqlConnection(connString);
                conn.Open();
                SqlCommand com = new SqlCommand(sql, conn);
                SqlDataReader dr = com.ExecuteReader(CommandBehavior.CloseConnection);
                return dr;

            }
            catch (Exception)
            {
                throw;
            }
            #endregion
        }

        /// <summary>
        /// 增删改
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        private int ExecuteNonQuery(string sql)
        {
            #region 访问数据库代码
            //打开数据库
            string connString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ToString();
            try
            {
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    conn.Open();
                    SqlCommand com = new SqlCommand(sql, conn);
                    int result = com.ExecuteNonQuery();
                    com.Dispose();
                    conn.Close();
                    return result;
                }
            }
            catch (Exception)
            {
                throw;
            }
            #endregion
        }
        #endregion
    }
}